package com.faxsun.service.handlers;

import java.math.BigDecimal;
import java.util.Map;

import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.catalog.domain.CategoryProductXref;
import org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl;
import org.broadleafcommerce.core.catalog.domain.Product;
import org.broadleafcommerce.core.catalog.domain.ProductBundle;
import org.broadleafcommerce.core.catalog.domain.Sku;
import org.broadleafcommerce.openadmin.dto.Entity;
import org.broadleafcommerce.openadmin.dto.FieldMetadata;
import org.broadleafcommerce.openadmin.dto.PersistencePackage;
import org.broadleafcommerce.openadmin.dto.PersistencePerspective;
import org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao;
import org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper;

/**
 * 产品类持久化实现
 * @author zhaoteng.song@faxsun.com
 *
 */
public class ProductCustomerPersistenceHandler
		extends
		org.broadleafcommerce.admin.server.service.handler.ProductCustomPersistenceHandler {

	@Override
	public Entity add(PersistencePackage persistencePackage,
			DynamicEntityDao dynamicEntityDao, RecordHelper helper)
			throws ServiceException {
		Entity entity = persistencePackage.getEntity();
		try {
			PersistencePerspective persistencePerspective = persistencePackage
					.getPersistencePerspective();
			Product adminInstance = (Product) Class
					.forName(entity.getType()[0]).newInstance();
			Map<String, FieldMetadata> adminProperties = helper
					.getSimpleMergedProperties(Product.class.getName(),
							persistencePerspective);

			if (adminInstance instanceof ProductBundle) {
				removeBundleFieldRestrictions((ProductBundle) adminInstance,
						adminProperties, entity);
			}

			adminInstance = (Product) helper.createPopulatedInstance(
					adminInstance, entity, adminProperties, false);

			adminInstance = (Product) dynamicEntityDao.merge(adminInstance);

			CategoryProductXref categoryXref = new CategoryProductXrefImpl();
			categoryXref.setCategory(adminInstance.getDefaultCategory());
			categoryXref.setProduct(adminInstance);
			categoryXref.setDisplayOrder(new BigDecimal(9999));
			if (adminInstance.getDefaultCategory() != null
					&& !adminInstance.getAllParentCategoryXrefs().contains(
							categoryXref)) {
				categoryXref = (CategoryProductXref) dynamicEntityDao
						.merge(categoryXref);
				adminInstance.getAllParentCategoryXrefs().add(categoryXref);
			}

			// Since none of the Sku fields are required, it's possible that the
			// user did not fill out
			// any Sku fields, and thus a Sku would not be created. Product
			// still needs a default Sku so instantiate one
			if (adminInstance.getDefaultSku() == null) {
				Sku newSku = catalogService.createSku();
				adminInstance.setDefaultSku(newSku);
				adminInstance = (Product) dynamicEntityDao.merge(adminInstance);
			}

			// also set the default product for the Sku
			adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
			dynamicEntityDao.merge(adminInstance.getDefaultSku());

			return helper.getRecord(adminProperties, adminInstance, null, null);
		} catch (Exception e) {
			throw new ServiceException("Unable to add entity for "
					+ entity.getType()[0], e);
		}
	}
}
